const db_promise = require("../config/db.config.js");
const logger = require('../util/logger');

async function getBotActionsAndResponses(req, res, next) {
	
  logger.winston.info('actions.getBotActionsAndResponses');
  
  var actionIds = [];
  var actions   = [];
  
  try {
    	
    	const [rows,fields] = await db_promise.query("select * from actions where bot_id = ? order by action_id desc",[req.query.bot_id]);    	
      
      console.log(rows);
      
      actions = rows;
      
      console.log(actions);
      
      for (var i = 0; i < rows.length; i++) {
        actionIds.push(rows[i].action_id);
      }
          	
  }catch(err){

	    res.status(400).send({
	      message: err
	    });  

  }    

  	
		  try {
		    	
				  if (actionIds.length > 0) {
				  	    	
				    	const [rows,fields] = await db_promise.query("select * from responses where action_id in (" + actionIds.splice(",") + ")  order by action_id desc");    	
				      
				      res.status(200).json([{actions: actions, responses: rows}]);
						          	
				  }else{
				  	
					    res.status(200).json([{actions: actions, responses: []}]);
					    
					}			          	
		          	
		  }catch(err){
		
			    res.status(400).send({
			      message: err
			    });  
		
		  }  
	
    
    

  

}

async function createAction(req, res, next) {
  logger.winston.info('actions.createAction');
  
    try {
    	
			  const [rows,fields] = await db_promise.query("insert into actions (action_name, bot_id) values (?,?)", [req.body.action_name, req.body.bot_id]);

        console.log(rows);			  
        if (rows.affectedRows >= 1){ 
			      res.status(200).json({ status: 'success', message: 'Updated' });
			  }else{
			  	  res.status(200).json({ status: 'no data', message: 'Updated' });
			  }    


  
    }catch(err){

		    res.status(400).send({
		      message: err
		    });  

	  }    
  

}


async function removeAction(req, res, next) {
  logger.winston.info('actions.removeAction');

  
    try {
    	
			  const [rows,fields] = await db_promise.query("delete from actions where action_id = ?", [req.query.action_id]);

        console.log(rows);			  
        if (rows.affectedRows >= 1){ 
			      res.status(200).json({ status: 'success', message: 'Updated' });
			  }else{
			  	  res.status(200).json({ status: 'no data', message: 'Updated' });
			  }    


  
    }catch(err){

		    res.status(400).send({
		      message: err
		    });  

	  }  
  
}


async function updateAction(req, res, next) {
  logger.winston.info('actions.updateAction');

  
    try {
    	
			  const [rows,fields] = await db_promise.query("update actions set action_name = ? where action_id = ?", [req.body.action_name, req.body.bot_id]);

        console.log(rows);			  
        if (rows.affectedRows >= 1){ 
			      res.status(200).json({ status: 'success', message: 'Updated' });
			  }else{
			  	  res.status(200).json({ status: 'no data', message: 'Updated' });
			  }    


  
    }catch(err){

		    res.status(400).send({
		      message: err
		    });  

	  }  
  
  
}

module.exports = {
  getBotActionsAndResponses,
  createAction,
  updateAction,
  removeAction
};
